#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.db import models
from .widgets import StarkImageFormField


class StarkImageField(models.ImageField):
    """
    添加和编辑页面支持图片预览
    """

    def __init__(self, width="100px", height="100px", *args, **kwargs):
        self.width = width
        self.height = height

        super(StarkImageField, self).__init__(*args, **kwargs)

    def formfield(self, **kwargs):
        defaults = {
            'form_class': self._get_form_class(),
            'width': self.width,
            'height': self.height,
        }
        defaults.update(kwargs)
        return super(StarkImageField, self).formfield(**defaults)

    @staticmethod
    def _get_form_class():
        return StarkImageFormField
